1 module unde.games.dizzy.omega.star;
2 
3 import derelict.opengl3.gl;
4 import std.conv;
5 import std.format;
6 import unde.games.object;
7 import unde.games.renderer;
8 import unde.global_state;
9 
10 class Star:StaticGameObject
11 {
12     static int num;
13     int number;
14     bool taken;
15     
16     this(MainGameObject root, float[3] coords)
17     {
18         x = coords[0];
19         y = coords[1];
20         z = coords[2];
21 
22         number = num++;
23         
24         super(root);
25     }
26 
27     bool maybe_taken(GlobalState gs, StaticGameObject the_hero)
28     {
29         return !taken && the_hero.x-1.5 <= x && x <= the_hero.x+1.5 &&
30                the_hero.y <= y+1.0 && y+1.0 <= the_hero.y+2.5 &&
31                the_hero.z-1.0 <= z && z <= the_hero.z+1.0;
32     }
33 
34     bool set_light(GlobalState gs)
35     {
36         if (!taken && x-root.scrx >= -15 && x-root.scrx < 15 &&
37             y-root.scry >= -8.5 && y-root.scry < 8.5)
38         {
39             glEnable(GL_LIGHT1);
40             glLightfv(GL_LIGHT1, GL_POSITION, [x-root.scrx, y-root.scry+0.35f, z, 1.0f].ptr);
41             return true;
42         }
43         
44         return false;
45     }
46 
47     void take()
48     {
49         taken = true;
50     }
51 
52     void force_draw(GlobalState gs)
53     {
54         glPushMatrix();
55         glTranslatef(0, 10.0, 0.0);
56         glScalef(0.35, 0.35, 0.35);
57         recursive_render(gs, root.models["star-"~(cast(uint)(root.frame/7.5)%10).to!(string)]);
58         glPopMatrix();
59     }
60 
61     override void draw(GlobalState gs)
62     {
63         if (!taken && x-root.scrx >= -15 && x-root.scrx < 15 &&
64             y-root.scry >= -8.5 && y-root.scry < 8.5)
65         {
66             glPushMatrix();
67             glTranslatef(x, y, z);
68             glScalef(0.35, 0.35, 0.35);
69             recursive_render(gs, root.models["star-"~(cast(uint)(root.frame/7.5)%10).to!(string)]);
70             glPopMatrix();
71         } 
72     }    
73 
74     override bool tick(GlobalState gs)
75     {
76         return true;
77     }
78 
79     override void load(string[string] s)
80     {
81         string key = format("star-%03d", number);
82         taken = (key in s) !is null;
83     }
84 
85     override void save(ref string[string] s)
86     {
87         if (taken)
88         {
89             string key = format("star-%03d", number);
90             s[key] = "taken";
91         }
92     }    
93 }